package logic

import (
	"context"
	"fmt"

	"github.com/zeromicro/x/errors"

	"go-weixin/app/guitar/model"
	"go-weixin/app/guitar/rpc/internal/svc"
	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
)

type GuitarGetImagesLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGuitarGetImagesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarGetImagesLogic {
	return &GuitarGetImagesLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GuitarGetImagesLogic) GuitarGetImages(in *guitar.GuitarGetImagesReq) (*guitar.GuitarGetImagesResp, error) {

	var limit int64
	var offset int64
	limit = 10
	offset = (in.Page - 1) * 10
	gm := make([]model.GuitarJoinImageGroup, 0)
	err := l.svcCtx.ChenDb.Join("LEFT", "guitar_image", "guitar.id = guitar_image.guitar_id").
		Where("guitar.id = ?", in.Id).
		Limit(int(limit), int(offset)).
		Find(&gm)
	if err != nil {
		return nil, errors.New(8000, "guitar.get.images.mysql:"+err.Error())
	} else {
		logx.Info("查询成功")
		fmt.Println(gm)
	}
	// var data *guitar.GuitarJoinImageBase
	data := new(guitar.GuitarJoinImageBase)
	if len(gm) > 0 {
		data.Id = gm[0].GuitarModel.Id
		data.Title = gm[0].GuitarModel.Title
		for _, m := range gm {
			if m.GuitarImageModel.Id != 0 {
				data.ImageList = append(data.ImageList, &guitar.GuitarImageBase{
					Id:       m.GuitarImageModel.Id,
					GuitarId: m.GuitarImageModel.GuitarId,
					Url:      m.GuitarImageModel.Url,
				})
			}
		}
	}
	return &guitar.GuitarGetImagesResp{Data: data}, nil
}
